package com.lsw.choiceForm;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;


import com.lsw.R;
import com.lsw.pickerview.ArrayWheelAdapter;
import com.lsw.pickerview.WheelView;

import java.util.ArrayList;
import java.util.List;


/**
 * 选择形式
 *
 * @author Mars
 */
public class ChoiceFormPopActivity extends Activity {

    public static void startActivityforResult(Activity activity,
                                              String defaultForm, String title, int requestCode, List<ChoiceBean> list) {
        Intent intent = new Intent(activity, ChoiceFormPopActivity.class);
        intent.putExtra("defaultForm", defaultForm);
        intent.putExtra("title", title);
        mList = (ArrayList<ChoiceBean>) list;
        activity.startActivityForResult(intent, requestCode);
    }

    private WheelView wheelForm;
    private TextView tvcacle, tvok, tvtitle;
    private String defaultForm, title;
    private static ArrayList<ChoiceBean> mList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.include_wheeview_1);
        getWindow().setLayout(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);

        getList();
        initView();
        initData();
    }

    private void initView() {
        wheelForm = (WheelView) findViewById(R.id.wheeView_center);
        tvcacle = (TextView) findViewById(R.id.tvcacle);
        tvok = (TextView) findViewById(R.id.tvok);
        tvtitle = (TextView) findViewById(R.id.tvtitle);
    }

    private ArrayWheelAdapter<ChoiceBean> adapter;
    private ArrayList<ChoiceBean> formList;

    /**
     * 初始化服务形式列表数据
     */
    private void getList() {
        formList = new ArrayList<ChoiceBean>();
        formList.addAll(mList);
    }

    /**
     * 遍历查找初始值
     *
     * @return
     */
    private int checkDefaultAgeIndex() {
        int index = 0;
        if (!TextUtils.isEmpty(defaultForm)) {
            for (int i = 0; i < formList.size(); i++) {
                ChoiceBean bean = formList.get(i);
                if (defaultForm.equals(bean.getField_name())) {
                    index = i;
                }
            }
        }
        return index;
    }

    private void initData() {
        defaultForm = getIntent().getStringExtra("defaultForm");
        title = getIntent().getStringExtra("title");
        if (!TextUtils.isEmpty(title)) {
            tvtitle.setText(title);
        }
        if (formList == null) {
            return;
        }
        adapter = new ArrayWheelAdapter<ChoiceBean>(formList);
        wheelForm.setAdapter(adapter);
        wheelForm.setCurrentItem(checkDefaultAgeIndex());

        tvcacle.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransition(0, R.anim.bottom_end);
            }
        });
        findViewById(R.id.layout_parent).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        finish();
                        overridePendingTransition(0, R.anim.bottom_end);
                    }
                });
        tvok.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ChoiceBean form = adapter.getItem(wheelForm
                        .getCurrentItem());
                if (form == null) {
                    return;
                }
                Intent data = new Intent();
                data.putExtra("name", form.getField_name());
                data.putExtra("value", form.getField_value());
                data.putExtra("position", wheelForm.getCurrentItem());
                setResult(RESULT_OK, data);
                finish();
                overridePendingTransition(0, R.anim.bottom_end);
            }
        });
    }
}
